﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using System.Threading.Tasks;
using System.Windows.Threading;
using RaisingForce.Raise.Hardware;
using RaisingForce.Raise.Presentation.Localization;
using RaisingForce.Raise.Presentation.ViewModel;
using RaisingForce.Raise.Presentation.ViewModel.Extensions;
using RaisingForce.RaiseHardwareTest.Properties;

namespace RaisingForce.RaiseHardwareTest.Hardware
{
    internal static class HardwareDispatcher
    {
        #region Static Methods

        public static void InvokeAsync(
            ViewModelBase viewModel,
            Action action,
            Action<ViewModelBase, Exception> exceptionReaction)
        {
            Contract.Requires(viewModel != null);
            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    LocalizationManager.InvokeInLocalizationContext(action);
                }
                catch (OutOfMemoryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (exceptionReaction != null)
                    {
                        dispatcher.Invoke(exceptionReaction, viewModel, ex);
                    }
                }
            });
        }

        public static void InvokeAsync(
            ViewModelBase viewModel,
            Action action)
        {
            Contract.Requires(viewModel != null);
            InvokeAsync(
                viewModel,
                action,
                new Action<ViewModelBase, Exception>(ShowException));
        }

        public static string GetExceptionMessage(Exception ex, out string caption)
        {
            Contract.Requires(ex != null);
            RobotHardwareException robotException =
                ex as RobotHardwareException;
            caption = robotException != null ?
                Resources.Error_Hardware_Caption :
                Resources.Error_Normal_Caption;
            string message;
#if DEBUG
            message = ex.ToString();
#else
            if (robotException != null
                && robotException.InnerException != null)
            {
                message = string.Concat(
                    ex.Message,
                    Environment.NewLine,
                    robotException.InnerException.Message);
            }
            else
            {
                message = ex.Message;
            }
#endif
            return message.Trim();  // message can begins or ends with EOF
        }

        public static void ShowException(ViewModelBase viewModel, Exception ex)
        {
            Contract.Requires(viewModel != null);
            string caption;
            string message = GetExceptionMessage(ex, out caption);
            viewModel.ShowMessage(message, caption, isErrorDialog: true);
        }

        #endregion
    }
}
